package org.box.metadata.shell.message;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.box.metadata.cli.annotation.Command;
import org.box.metadata.cli.util.HelpFormatter;
import org.box.metadata.shell.exception.ValidationException;

public class HelpMessageFormatter implements MessageFormatter {

	public static final String HELP_MESSAGE_FORMATTER = "HelpMessageFormatter";

	/*
	 * (non-Javadoc)
	 * @see org.box.metadata.shell.message.ShellMessage#getMessage()
	 */
	@SuppressWarnings({ "rawtypes" })
	public String format(Command c, Class[] cfgClasses, Object... args) {
		StringWriter writer = new StringWriter();
		for (Object msg : args) {
			if (msg != null)
				if (msg instanceof ValidationException) {
					String s = ((ValidationException) msg).getMessage();
					if (s != null && !s.trim().isEmpty())
						writer.write(s + "\n");
				} else
					writer.write("" + msg + "\n");
		}
		
		HelpFormatter formatter = new HelpFormatter(cfgClasses);
		formatter.setIncCommonOptions(false);
		formatter.printHelp(new PrintWriter(writer));

		return writer.toString();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.box.metadata.shell.message.MessageFormatter#getId()
	 */
	public String getId() {
		return HELP_MESSAGE_FORMATTER;
	}
}
